package alipay.openplatform.edu.campus.jiuye.common.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * @Remark see class name
 * @Author pangyiyang
 * @Date 16/5/17 下午2:48
 */
public class AESUtil {

    private static final String AESTYPE = "AES/ECB/PKCS5Padding";

    public static String encrypt(String plainText,String keyStr) {
        byte[] encrypt = null;
        try {
            Key key = generateKey(keyStr);
            Cipher cipher = Cipher.getInstance(AESTYPE);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            encrypt = cipher.doFinal(plainText.getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(Base64.encodeBase64(encrypt));
    }

    public static String decrypt(String encryptData,String keyStr) {
        byte[] decrypt = null;
        try {
            Key key = generateKey(keyStr);
            Cipher cipher = Cipher.getInstance(AESTYPE);
            cipher.init(Cipher.DECRYPT_MODE, key);
            decrypt = cipher.doFinal(Base64.decodeBase64(encryptData));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(decrypt).trim();
    }

    private static Key generateKey(String key) throws Exception {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
            return keySpec;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    public static void main(String[] args) {

        String keyStr = "b0fa5ac21055476d";

        String plainText = "GrHFF5daCq7irD/fLlDgS5NnmE8cc6hC1IR63G/h6FUu04T09uR57etMRM5jtvazIktZpn+h/YQQkut93a0ArZz8QHMPh8G4amSNfvfRVZmYiZsDZylp0b6bgb9WfsgrzMDlNtxhZcUu/aMqEml5cKiNHVfUs1h+raT9t4U0YLNSTr6bUtGO7I1upgOqyJRyNPBzCNH4ezQeQSr6cLfetuzEBloUXpkoU55B1kcKmANJaMoJf5OVG0bZKDeiGvKspqIQ4NkSyOnu6RRi0I1lK+tTKBh9OKWfsD8dYdGtfDxcc0ZkfwtS/L27AJOKxklmxo3LIjnQjpk1+yktq6FUY3IRwC53wijnzg4tvQa3mymVc62K/1ENLvEIUI1ya4mvmwTbE95NUEAuHzxHexyrcHqZ4Msk3z5P8WJI87o4j0I=";

//        String encText = encrypt(keyStr, plainText);
        String decString = decrypt(plainText , keyStr);

//        System.out.println(encText);
        System.out.println(decString);


        String s = "{\"rtnMsg\":\"收款账户有误或不存在！\",\"payeeName\":\"贾梦婷\",\"amount\":\"20000\",\"bankNo\":\"201606230091825958\",\"bizOrderNo\":\"160623162153408_0648\",\"tradeStatus\":\"FAIL\",\"bankOrderNo\":\"93120160623163049223398586\",\"rtnCode\":\"F\",\"bizCode\":\"QUFENQI\",\"finishTime\":\"2016-06-23 16:30:49\",\"payeeCardNo\":\"18315911382\",\"merchantNo\":\"2088221502108296\"}" ;
        System.err.println(AESUtil.encrypt(s , keyStr));



        String tt = "elmnIfJnhAU+1yyiNLho/YEYtysphv+dAHLvbUXr6ksZ3f/mpRZu0ejhWyAxfgmwkNBIA7CpPNq1yiNrZM7K9TtcyxTPvBB3/Ffa59RuI7DjxEcN+kWVcR8cU9K3NqvcQeT+bl8C19qoWnCUZgayjekjTso4xlnwSgmMGt86+gyIQlbjxqVy5cxIb6qQ09kPBXirik4lslcPfyJ+xf4eBz9SkoCS6RMWOYNl7IZVw/2snv+ieAETOs+rNYjPnIsAi5Gt7h2Kl6jNM/esAVHeJMOnJ/FAt54F4X9YUHce7Wcs34vsQFP8mze0mefos/SW5Vr/bD4k8efY/4TyB6C+XUnSTBuKdHV+D5Y5rNb2NVVsKl4XLAd+uI0v7pLvTMpa6Knxa4R1WWxSCyn6MPMZS7dwj4oB618f+NrSwtfCczFVrE6YzXfyxMk4NyMqTOu143NqPiFVOvYXpW2LBGopAg==\n" ;
        System.err.println(AESUtil.decrypt(tt , keyStr));

    }

}
